package com.sdry.web.controller.jyy;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdry.model.jyy.Receive;
import com.sdry.model.jyy.ReceiveDetail;
import com.sdry.model.lz.Materiel;
import com.sdry.service.jyy.ReceiveDetailService;
import com.sdry.service.jyy.ReceiveService;
import com.sdry.utils.DateJsonValueProcessor;
import com.sdry.utils.DateUtil;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;
/**
 *
 *@ClassName: ReceiveDetailController
 *@Description: 收货计划详情
 *@Author jyy
 *@Date 2019-04-19 14:30:17
 *@version 1.0
*/
@Controller
@RequestMapping("/receiveDetail")
public class ReceiveDetailController{
	@Resource ReceiveService ReceiveService;
	@Resource ReceiveDetailService ReceiveDetailService;
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public ReceiveDetail queryById(Long id) {
		ReceiveDetail param = ReceiveDetailService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<ReceiveDetail> queryAllByMution(ReceiveDetail param) {
		List<ReceiveDetail> list = ReceiveDetailService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(ReceiveDetail param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<ReceiveDetail> list = ReceiveDetailService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = ReceiveDetailService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("data", jsonArray);
		result.put("count", total);
		ResponseUtil.write(response, result);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(ReceiveDetail param, HttpServletResponse response) throws Exception{
		
		String ids = "";
		ReceiveDetail receiveDetail = new ReceiveDetail();
		receiveDetail.setReceiveNumber(param.getReceiveNumber());
		
		if(param.getMid() != null) {
			receiveDetail.setMid(param.getMid());
			List<ReceiveDetail> details = ReceiveDetailService.queryAllByMution(receiveDetail);
			
			if(details.size() > 0) {
				for (int i = 0; i < details.size(); i ++) {
					if(i == 0){
		                ids += details.get(i).getId();
		            }else{
		                ids += "," + details.get(i).getId();
		            }
					ReceiveDetailService.delete(ids);
				}
			}
		}else {
			param.setMid(param.getId());
		}
		
		Long id = ReceiveDetailService.insert(param);
		response.getWriter().print(id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(ReceiveDetail param,HttpServletResponse response) throws Exception{
		Integer count = ReceiveDetailService.update(param);
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = ReceiveDetailService.delete(ids);
		response.getWriter().print(count);
	}
	
	/**
	 * 根据供应商 名称 查询相关物料信息
	 * @param client 供应商 名称
	 * @throws Exception 
	 */
	@RequestMapping("/materielList")
	public void materielList(String id, HttpServletResponse response) throws Exception {
		//转json数据
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		if(StringUtil.isNotEmpty(id) && !"null".equals(id)) {
			List<Materiel> list = ReceiveService.materielList(Long.parseLong(id));
			JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
			//添加入result
			result.put("code", 0);
			result.put("data", jsonArray);
			result.put("count", list.size());
			ResponseUtil.write(response, result);
		} else {
			//添加入result
			result.put("code", 0);
			result.put("data", "");
			result.put("count", 0);
			ResponseUtil.write(response, result);
		}
	}
	
	//-------------------------------------------------app---------------------------------------------------------------
	
	/**
	 * 根据收货单号查询收货详情
	 * @param receiveNumber 收货单号
	 * @param response 响应
	 * @throws Exception 
	 */
	@RequestMapping("/selectDtailByNumber")
	public void selectDtailByNumber(String receiveNumber,HttpServletResponse response) throws Exception {
		
		ReceiveDetail param = new ReceiveDetail();
		param.setReceiveNumber(receiveNumber);
		List<ReceiveDetail> list = ReceiveDetailService.queryAllByMution(param );
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据id修改
	 * @param param
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/updateById")
	public void  updateById(String json,HttpServletResponse response) throws Exception {
		
		int affact = 0;
		Gson gson = new Gson();
		List<ReceiveDetail> list= gson.fromJson(json,new TypeToken<List<ReceiveDetail>>() {}.getType());
		
		for (ReceiveDetail receiveDetail : list) {
			affact = ReceiveDetailService.update(receiveDetail);
		}
		
		ReceiveDetail receiveDetail = list.get(0);
		//判断一级中的二级是否都已收货
		int count =  ReceiveDetailService.countState(receiveDetail.getReceiveNumber());
		//更改一级状态
		if(count == 0) {
			Receive receive = new Receive();
			receive.setReceiveNumber(receiveDetail.getReceiveNumber());
			List<Receive> receives = ReceiveService.queryAllByMution(receive);
			Receive receive2 = receives.get(0);
			receive2.setState(1);
			receive2.setCreateDate(DateUtil.dateFormat1());
			receive2.setCreateName("app");
			ReceiveService.update(receive2);
		}
		
		JSONObject jsonObject = new JSONObject();
		if (affact>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", "提交成功");
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "提交失败");
		    jsonObject.put("data", "0");
		}
		ResponseUtil.write(response, jsonObject);
	}
	
}
